# main.py
import relog as rl

rl.write("sra.log","main:启动应用程序...")

import tkinter as tk
from item_viewer import ItemViewer
import configparser
import os
from settings_applier import apply_settings

# 读取设置函数
def load_settings():
    config = configparser.ConfigParser()
    config_file = "set.ini"
    
    # 默认设置
    default_settings = {
        'background_color': '#f0f0f0',
        'foreground_color': '#f0f0f0',
        'text_color': '#000000',
        'transparency': '1.0',
        'always_on_top': 'False'
    }
    
    # 如果设置文件存在，则读取
    if os.path.exists(config_file):
        config.read(config_file, encoding='utf-8')
        if 'SETTINGS' in config:
            # 合并默认设置和用户设置
            for key in default_settings:
                if key in config['SETTINGS']:
                    default_settings[key] = config['SETTINGS'][key]
    
    return default_settings

# 加载设置
settings = load_settings()

root = tk.Tk()

# 应用设置到主窗口
apply_settings(root, settings)

# 确保主窗口不会最小化
root.deiconify()

app = ItemViewer(root, settings)  # 传递设置给ItemViewer
rl.write("sra.log","main:应用程序初始化完成，进入主循环")
root.mainloop()
rl.write("sra.log","main:应用程序已退出")
rl.clean_mem()